/**
 * 
 */
package com.ez.supervise.service.impl;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.core.Page;
import com.ez.supervise.dao.OrgGoalSettingDao;
import com.ez.supervise.model.biz.OrgGoalSetting;
import com.ez.supervise.service.OrgGoalSettingService;

/**
 * @author searph.wang
 * @email searph.wang@gmail.com
 * @Created at 2009-7-22
 * 
 */
public class OrgGoalSettingServiceImpl implements OrgGoalSettingService {
    private OrgGoalSettingDao orgGoalSettingDao;

    // public int update(OrgGoalSetting org) {
    // return sqlMapClientTemplate.update("sys.updateOrgGoalSetting", org);
    // }

    public void setOrgGoalSettingDao(OrgGoalSettingDao orgGoalSettingDao) {
	this.orgGoalSettingDao = orgGoalSettingDao;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#delete(java.lang.Long)
     */
    public void delete(Long id) {
	orgGoalSettingDao.delete(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#insert(java.lang.Object)
     */
    public void insert(Object entity) {
	orgGoalSettingDao.insert(entity);

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#update(java.lang.Object)
     */
    public void update(Object entity) {
	orgGoalSettingDao.update(entity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#get(java.lang.Long)
     */
    public Object get(Long id) {
	return (OrgGoalSetting) orgGoalSettingDao.get(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.OrgGoalSettingService#findAll()
     */
    public List findAll() {
	return orgGoalSettingDao.findAll();
    }

    public void findOrgGoalSettingByPage(Page page) {
	page.setRoot(orgGoalSettingDao.findOrgGoalSettingByPage(page));
	page.setTotalProperty(orgGoalSettingDao.findOrgGoalSettingByCount(page));
    }

    public void save(String orgIds, String goalIds, String goalType) {
	String[] orgId = orgIds.split(",");
	String[] goalId = goalIds.split(",");
	for (String oId : orgId) {
	    if (NumberUtils.isNumber(oId)) {
//		orgGoalSettingDao.delOrgGoalSettingByOrgId(Long.valueOf(oId));
		for (String gId : goalId) {
		    if (NumberUtils.isNumber(gId)) {
			OrgGoalSetting orgGoalSetting = new OrgGoalSetting(Long.valueOf(oId), Long.valueOf(gId),
				goalType);
			int count = orgGoalSettingDao.countExistOrgGoalSetting(orgGoalSetting);
			if(count == 0) {
			    orgGoalSettingDao.insert(orgGoalSetting);
			}
		    }
		}
	    }
	}
    }
}
